#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>
 
void margin (int n)
{
  while (n-- > 0) printf(".");
  return;
}
 
void print_block (value v,int m)
{
  unsigned int			size, i;
  margin(m);
  if (Is_long(v))
    {
      printf("valeur immédiate (%d)\n", Int_val(v));
      return;
    };
  printf ("valeur construite: taille=%d\n", size=Wosize_val(v));
  margin(m+2);
  switch (Tag_val(v))
    {
    case Closure_tag :
      printf("Cloture\n");
      break;
    case String_tag :
      printf("string: %s (%s)\n", String_val(v),(char *) v);
      break;
    case Double_tag:
      printf("float: %g\n", Double_val(v));
      break;
    case Double_array_tag :
      printf ("float array: ");
      for (i=0;i<size/Double_wosize;i++)
	printf("  %g", Double_field(v,i));
      printf("\n");
      break;
    case Abstract_tag :
      printf("abstract type\n");
      break;
    case Custom_tag :
      printf("custom type\n");
      break;
    default:
      if (Tag_val(v)>=No_scan_tag)
	{
	  printf("unknown tag");
	  break;
	};
      printf("structured block (tag=%d):\n",Tag_val(v));
      for (i=0;i<size;i++)
	print_block(Field(v,i),m+4);
    }
  return ;
}
 
value inspect_block (value v)
{
  print_block(v,2);
  fflush(stdout);
  return v;
}
